<?php
namespace Cms\Controller;
use Cms\Controller\BaseController;
class JxroomsController extends BaseController {
	
	public function _initialize() {
		//你可以在此覆盖父类方法	
		parent::_initialize();
		$this->assign('useroath',$_SESSION['CMS']['user']['oath']);
	}
	
	public function index(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'代理商管理',
				'url'=>U('Cms/Jxrooms/#')
			),
			'1'=>array(
				'name'=>'代理房间号审核',
				'url'=>U('Cms/Jxrooms/index')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('rooms_jx_apply','t_');
		$p=$_GET['p']?$_GET['p']:1;
		$search=I('search')?I('search'):'';
		$stype=I('stype')?I('stype'):1;
		if($stype==1){
			if($search){
				$map['joel_vip.nickname']=array('like',"%$search%");
				$this->assign('search',$search);
			}			
			$this->assign('stype',1);
		}
		if($stype==2){
			if($search){
				$map['t_rooms_jx_apply.room_id']=array('eq',"$search");
				$this->assign('search',$search);
			}
			$this->assign('stype',2);
		}
		$start = I("startdate");
		$end = I("enddate");
		if($start&&$end){
			$map['t_rooms_jx_apply.game_create_time']=array('between',array($start." 00:00:00",$end." 23:59:59"));
		}elseif($start){
			$map['t_rooms_jx_apply.game_create_time']=array('egt',$start." 00:00:00");
		}elseif ($end){
			$map['t_rooms_jx_apply.game_create_time']=array('elt',$end." 23:59:59");
		}
		
		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		$map['t_rooms_jx_apply.jx_status'] = array('in',array(0,-1));
		$join = array("joel_vip ON joel_vip.id=t_rooms_jx_apply.jxuser_id");
		$cache=$m->join($join)->where($map)->page($p,$psize)->order("t_rooms_jx_apply.create_time DESC")->field("joel_vip.nickname,t_rooms_jx_apply.room_id roomid,t_rooms_jx_apply.id,t_rooms_jx_apply.jx_status,t_rooms_jx_apply.game_create_time")->select();
		$count=$m->join($join)->where($map)->count();
		$this->getPage($count, $psize, 'Joel-loader', '审核列表','Joel-search');
		$this->assign('cache',$cache);		
		$this->display();
	}
	
	public function setjxstatus(){
		$id=$_REQUEST['id'];//必须使用get方法
		$status = $_REQUEST['status'];
		$m=M('rooms_jx_apply','t_');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		if(!in_array($status,array(1,-1))){
			$info['status']=0;
			$info['msg']='设置状态有误!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);	
		if($status==1){//通过
			$wh['jx_status'] = array('in',array(-1,0));//未审核或否决的可通过
			$notupdate = array();
			$re = 1;
			$roomsmodel = M("rooms","t_");
			if(is_array($ids)&&count($ids)==1){//单条
				$wh['id'] = $ids[0];
				$roominfo = $m->where($wh)->find();
				if($this->checkconfirmed($roominfo['room_uuid'])){//此房间已审核过
					$info['status']=0;
					$info['msg']='此房间已被指定经销商!';
					$this->ajaxReturn($info);
				}
				$re = $m->where($wh)->save(array('jx_status'=>$status));
				if($re){
					$roomsmodel->where(array('uuid'=>$roominfo['room_uuid']))->save(array('jxuser_id'=>$roominfo['jxuser_id']));
				}
			}else{//多条		
				foreach ($ids as $v){
					$wh['id'] = $v; 
					$roomjxinfo = $m->where($wh)->find();
					if($m->where(array('room_uuid'=>$roomjxinfo['room_uuid']))->count()>1){//同一房间有两条以上申请需要单调审核
						$roominfo = $roomsmodel->where(array('uuid'=>$roomjxinfo['room_uuid']))->find();
						$notupdate[] = $roominfo['id'];
					}else{
						if($this->checkconfirmed($roomjxinfo['room_uuid'])){//此房间已审核过
							continue;
						}
						$wh['id'] = $v;
						$res = $m->where($wh)->save(array('jx_status'=>$status));
						if($res) {
							$roomsmodel->where(array('uuid'=>$roomjxinfo['room_uuid']))->save(array('jxuser_id'=>$roomjxinfo['jxuser_id']));
							$re[] = $res;
						}
					}
				}	
			}
		}else {//否决
			$wh = array('id'=>array('in',$ids));
			$wh['jx_status'] = 0;//只针对未审核
			$re = $m->where($wh)->save(array('jx_status'=>$status));//echo D()->_sql();
		}
		
		if($re){
			$info['status']=1;
			$info['msg']='设置成功!';
			if($notupdate){
				if(count($notupdate)==count($ids)){
					$info['status']=0;
					$info['msg']='设置失败!';
				}
				$notupdate =  array_flip(array_flip($notupdate));
				$info['msg'] .= '房间号为'.implode(",",$notupdate).'的有多人申请，请每条予以审核！';
			}
		}else{
			$info['status']=0;
			$info['msg']='设置失败!';
		}
		$this->ajaxReturn($info);
	}
	
	private function checkconfirmed($uuid){
		return M("rooms_jx_apply","t_")->where(array('room_uuid'=>$uuid,'jx_status'=>1))->count();
	}
	
	//未结算房间
	public function noaccount(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'代理商管理',
				'url'=>U('Cms/Jxrooms/#')
			),
			'1'=>array(
				'name'=>'未结算房间列表',
				'url'=>U('Cms/Jxrooms/noaccount')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('rooms_jx_apply','t_');
		$p=$_GET['p']?$_GET['p']:1;
		$search=I('search')?I('search'):'';
		$stype=I('stype')?I('stype'):1;
		if($stype==1){
			if($search){
				$map['joel_vip.nickname']=array('like',"%$search%");
				$this->assign('search',$search);
			}			
			$this->assign('stype',1);
		}
		if($stype==2){
			if($search){
				$map['t_rooms_jx_apply.room_id']=array('eq',"$search");
				$this->assign('search',$search);
			}
			$this->assign('stype',2);
		}
		$start = I("startdate");
		$end = I("enddate");
		if($start&&$end){
			$map['t_rooms_jx_apply.game_create_time']=array('between',array($start." 00:00:00",$end." 23:59:59"));
		}elseif($start){
			$map['t_rooms_jx_apply.game_create_time']=array('egt',$start." 00:00:00");
		}elseif ($end){
			$map['t_rooms_jx_apply.game_create_time']=array('elt',$end." 23:59:59");
		}
		
		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		$map['t_rooms_jx_apply.jx_status'] = 1;
		$map['t_rooms_jx_apply.account_status'] = 1;
		$join = array("joel_vip ON joel_vip.id=t_rooms_jx_apply.jxuser_id");
		$cache=$m->join($join)->where($map)->page($p,$psize)->order("t_rooms_jx_apply.create_time DESC")->field("joel_vip.nickname,t_rooms_jx_apply.room_id roomid,t_rooms_jx_apply.id,t_rooms_jx_apply.account_status,t_rooms_jx_apply.game_create_time,t_rooms_jx_apply.game_end_time,t_rooms_jx_apply.account*0.01 account")->select();
		$count=$m->join($join)->where($map)->count();
		$this->getPage($count, $psize, 'Joel-loader', '未结算列表','Joel-search');
		$this->assign('cache',$cache);		
		$this->display();
	}
	
	//结算
	public function setaccount(){
		$id=$_REQUEST['id'];//必须使用get方法
		$m=M('rooms_jx_apply','t_');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);
		$re = $m->where(array('id'=>array('in',$ids),'account_status'=>1))->save(array('account_status'=>2));
		if(!$re){
			$info['status']=0;
			$info['msg']='设置失败!';
		}else{
			$info['status']=1;
			$info['msg']='设置成功!';
		}
		$this->ajaxReturn($info);
	}
	
	//未结算房间
	public function account(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'代理商管理',
				'url'=>U('Cms/Jxrooms/#')
			),
			'1'=>array(
				'name'=>'业务汇总表',
				'url'=>U('Cms/Jxrooms/account')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('rooms_jx_apply','t_');
		$p=$_GET['p']?$_GET['p']:1;
		$search=I('search')?I('search'):'';
		$stype=I('stype')?I('stype'):1;
		if($stype==1){
			if($search){
				$map['joel_vip.nickname']=array('like',"%$search%");
				$this->assign('search',$search);
			}			
			$this->assign('stype',1);
		}
		if($stype==2){
			if($search){
				$map['t_rooms_jx_apply.room_id']=array('eq',"$search");
				$this->assign('search',$search);
			}
			$this->assign('stype',2);
		}
		$start = I("startdate");
		$end = I("enddate");
		if($start&&$end){
			$map['t_rooms_jx_apply.game_create_time']=array('between',array($start." 00:00:00",$end." 23:59:59"));
		}elseif($start){
			$map['t_rooms_jx_apply.game_create_time']=array('egt',$start." 00:00:00");
		}elseif ($end){
			$map['t_rooms_jx_apply.game_create_time']=array('elt',$end." 23:59:59");
		}
		
		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		$map['t_rooms_jx_apply.jx_status'] = 1;
		$map['t_rooms_jx_apply.account_status'] = array('egt',1);
		$join = array("joel_vip ON joel_vip.id=t_rooms_jx_apply.jxuser_id");
		$cache=$m->join($join)->where($map)->page($p,$psize)->order("t_rooms_jx_apply.create_time DESC")->field("joel_vip.nickname,t_rooms_jx_apply.room_id roomid,t_rooms_jx_apply.id,t_rooms_jx_apply.account_status,t_rooms_jx_apply.game_create_time,t_rooms_jx_apply.game_end_time,t_rooms_jx_apply.account*0.01 account")->select();
		$count=$m->join($join)->where($map)->count();
		$this->getPage($count, $psize, 'Joel-loader', '业务汇总表','Joel-search');
		$this->assign('cache',$cache);		
		$this->display();
	}
	
}